package com.css.ids.server.ticket;

import java.io.ByteArrayInputStream;
import java.util.Calendar;
import java.util.Date;

import com.css.ids.server.util.ZipByteUtil;
import com.css.util.Base64Util;
import com.css.util.Md5Util;

public class TicketGenerator {
	public static final int SECOND = Calendar.SECOND;
	public static final int MINUTE = Calendar.MINUTE;
	public static final int HOUR = Calendar.HOUR;
	public static final int DAY = Calendar.DAY_OF_YEAR;
	public static final int MONTH = Calendar.MONTH;
	public static final int YEAR = Calendar.YEAR;

	public static String generate(Date fromDate, int type, int value, String remark) throws Exception {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(fromDate);
		calendar.add(type, value);
		String key = encode(calendar.getTimeInMillis(), remark);
		return key;
	}

	public static String generate(int type, int value, String remark) throws Exception {
		Date fromDate = new Date();
		return generate(fromDate, type, value, remark);
	}

	private static String encode(long time, String remark) throws Exception {
		if (remark == null)
			remark = "";
		String key = time + remark;
		key = Md5Util.getMD5(key) + key;
		byte[] source = ZipByteUtil.zip(new ByteArrayInputStream(key.getBytes("utf-8")));
		return Base64Util.base64Encode(source);
	}

}
